package com.example.side_pull_menu.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.side_pull_menu.Fragmentkj7;
import com.example.side_pull_menu.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

public class Fragment_one extends Fragment {
    private TextView tv_name;
    private TextView tv_sex;
    private TextView tv_tel;
    private TextView tv_id;
    private TextView tv_time;
    private TextView tv_vehicle;
    private ListView lv;
    private BaseAdapter adapter;
    private List<Fragmentkj7>fragmentkj7s=new ArrayList<>();
    public Fragment_one() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one1, container, false);
        initView(view);
        initData(view);
        return view;
    }
    public interface BlogService{
        @GET("getmyinfo")
        Call<ResponseBody> getBlog();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private void initView(View view) {
        lv=(ListView)view.findViewById(R.id.lv);
        tv_name = (TextView) view.findViewById(R.id.tv_name);
        tv_sex = (TextView) view.findViewById(R.id.tv_sex);
        tv_tel = (TextView) view.findViewById(R.id.tv_tel);
        tv_id = (TextView) view.findViewById(R.id.tv_id);
        tv_time = (TextView) view.findViewById(R.id.tv_time);
        tv_vehicle = (TextView) view.findViewById(R.id.tv_vehicle);
    }
    public void initData(View view){
        Retrofit retrofit=new Retrofit.Builder()
                //设置目标的网络地址
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogService service=retrofit.create(BlogService.class);
        Call<ResponseBody>call=service.getBlog();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Gson gson = new Gson();
                    Fragmentkj7 carlist = gson.fromJson(response.body().string(), Fragmentkj7.class);
                    fragmentkj7s.add(carlist);
                    lv.setAdapter(new MeAdapter());
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }
    private class MeAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return fragmentkj7s.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1=(View)View.inflate(getContext(),R.layout.fragment_one,null);
            TextView tv_name=view1.findViewById(R.id.tv_name);
            TextView tv_time=view1.findViewById(R.id.tv_time);
            TextView tv_id=view1.findViewById(R.id.tv_id);
            TextView tv_sex=view1.findViewById(R.id.tv_sex);
            TextView tv_tel=view1.findViewById(R.id.tv_tel);
            TextView tv_vehicle=view1.findViewById(R.id.tv_vehicle);
            tv_name.setText("姓名:"+fragmentkj7s.get(0).get姓名());
            tv_time.setText("注册时间:"+fragmentkj7s.get(0).get注册时间());
            tv_id.setText("身份证号码:"+fragmentkj7s.get(0).get身份证号码());
            tv_sex.setText("性别:"+fragmentkj7s.get(0).get性别());
            tv_tel.setText("手机号码:"+fragmentkj7s.get(0).get手机号码());
            String strings = fragmentkj7s.get(0).get名下车辆().toString();
            tv_vehicle.setText("名下车辆:"+strings);
            return view1;
        }
    }
}

